package com.jrx.anytxn.customer.controller;

import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.customer.dto.customer.CmLimitAdjustLogRes;
import com.jrx.anytxn.customer.service.IAdjustLimitDetailService;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Api(tags = "页面调额明细接口")
@RestController
@RequestMapping(value = "/customer/adjustLimit")
public class AdjustLimitDetailController {

    private static final Logger logger = LoggerFactory.getLogger(AdjustLimitDetailController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IAdjustLimitDetailService adjustLimitDetailService;


    @ApiOperation(value = "根据租户ID,客户ID，主键ID查询调额详情", notes = "根据租户ID,客户ID，主键ID查询调额详情")
    @GetMapping(value = "/{tenantId}/{customerId}/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "customerId", value = "客户ID", required = true),
            @ApiImplicitParam(name = "id", value = "主键ID", required = true)})
    public TxnRespResult<CmLimitAdjustLogRes> getByTenIdAndCustId(@NotNull @PathVariable String tenantId,
                                                                  @PathVariable String customerId,@PathVariable Long id) {
        TxnRespResult<CmLimitAdjustLogRes> result = new TxnRespResult<>();
        CmLimitAdjustLogRes res;
        try {
            res = adjustLimitDetailService.findByTenIdAndCustId(tenantId, customerId, id);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


    @ApiOperation(value = "分页查询调额明细信息", notes = "分页查询调额明细信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<CmLimitAdjustLogRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize,
                                                                         @RequestParam String customerId, @RequestParam String idNumber) {
        TxnRespResult<TxnPage<CmLimitAdjustLogRes>> result = new TxnRespResult<>();
        TxnPage<CmLimitAdjustLogRes> response;
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            response = adjustLimitDetailService.findAllPageByTenantId(pageNum, pageSize,customerId,idNumber,tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

}
